package FlowGram.Application.Parser.Actions.Property;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Hashtable;

import FlowGram.Application.Parser.Actions.FinchAction;
import FlowGram.Application.Parser.Controllers.FinchController;
import FlowGram.Application.Parser.Utility.ClassFinder;


/**
 * 
 * @author thackray
 *
 */
public abstract class PropertyAction extends FinchAction {

	
	public String toHTML(String idPrefix){
		return this.toCode();
	}
	
	
	
	private static Hashtable<String, PropertyAction> properties;
	
	public static PropertyAction findProperty(String property){
		if(properties == null){
			properties = new Hashtable<String, PropertyAction>();
			findProperties();
		}
		if(properties.containsKey(property)){
			return properties.get(property);
		}
		return null;
		
	}
	private static void findProperties(){
		Class[] props = ClassFinder.findProperties();
		for(int i=0; i<props.length; i++){
			Method m;
			try {
				m = props[i].getMethod("toCode", null);
				Constructor c = props[i].getConstructor(null);	
				
				PropertyAction inst = (PropertyAction)c.newInstance(null);
				String name = (String)m.invoke(inst, new Object[]{});
				properties.put(name, inst);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
	}
	public abstract boolean getValue(FinchController controller);
	@Override
	public void execute(FinchController controller) {
		return; //nothing
	}
}